package pe.edu.upc.dsd.jjjar.upcbank.client;


import pe.edu.upc.dsd.jjjar.upcbank.client.mvc.AppView;
import pe.edu.upc.dsd.jjjar.upcbank.client.mvc.layouts.AfiliacionNueva;
import pe.edu.upc.dsd.jjjar.upcbank.client.mvc.layouts.AfiliacionesList;


import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.CardLayout;
import com.google.gwt.core.client.GWT;

public class AfiliacionView extends View{

	
	public static final String CONTENT_PANEL = "articuloPanel";
	
	private ContentPanel container;
	private CardLayout layout;
	private AfiliacionesList buscador;
	private AfiliacionNueva nuevo;
	
	
	public AfiliacionView(Controller controller) {
		super(controller);
	}
	
	@Override
	protected void handleEvent(AppEvent event) {
		if(event.getType() == AppEvents.Afiliaciones){
			GWT.log("Afiliaciones ....");
			initSearch();
		}else if(event.getType() == AppEvents.Afiliar){
			initNew();
		}
	}
	
	@Override
	protected void initialize() {
	
		ContentPanel mainPanel = (ContentPanel) Registry.get(AppView.MAIN_PANEL);
		
		container = new ContentPanel();
		container.setHeaderVisible(false);
		container.setBodyBorder(false);
		container.setBorders(false);
		layout = new CardLayout();
		container.setLayout(layout);
		
		mainPanel.add(container);
		Registry.register(CONTENT_PANEL, container);
	}

	
	public void initUI(){
		
		ContentPanel mainPanel = (ContentPanel) Registry.get(AppView.MAIN_PANEL);
		CardLayout layout = (CardLayout)mainPanel.getLayout();

		layout.setActiveItem(container);
		
	}

	public void initSearch(){

		initUI();
		
		int index =container.indexOf(buscador);
		if(index==-1){
			buscador = new AfiliacionesList(this);
			container.add(buscador);
			layout.setActiveItem(buscador);
			
		}else{
			layout.setActiveItem(buscador);
		}
	}
	
	

	public void initNew(){
	
		initUI();
		
		int index =container.indexOf(nuevo);
		if(index==-1){
			nuevo = new AfiliacionNueva(this);
			container.add(nuevo);
			layout.setActiveItem(nuevo);
			
		}else{
			layout.setActiveItem(nuevo);
		}
		
		
	}
	


	public void close(LayoutContainer c){
		container.remove(c);
	}
	
	public void closeAndBack(LayoutContainer c){
		close(c);
		initSearch();
	}

}
